package com.shiloop.sensorsampler;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created with Android Studio
 * Author   : Baofeng
 * Time     : 2015/3/3.14:51
 * Description:
 */
public class ArcView extends View {
    public static final int MAX_LENGTH = 200;

    private Paint mArcPaint;

    private final Object mDataLock = new Object();

    private float x[];
    private float y[];
    private float z[];

    private float mMaxX;
    private float mMaxY;
    private float mMaxZ;

    private RectF mRectF;

    int mDr = 360 / MAX_LENGTH;


    public ArcView(Context context) {
        super(context);
        init();
    }

    public ArcView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ArcView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ArcView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        x = new float[MAX_LENGTH];
        y = new float[MAX_LENGTH];
        z = new float[MAX_LENGTH];
        mArcPaint = new Paint();
        for (int j = 0; j < x.length; j++) {
            x[j] = 0;
            y[j] = 0;
            z[j] = 0;
        }
        mMaxX = 0.0f;
        mMaxY = 0.0f;
        mMaxZ = 0.0f;

        mRectF = new RectF();
    }

    public void reset() {
        synchronized (mDataLock) {
            for (int j = 0; j < x.length; j++) {
                x[j] = 0;
                y[j] = 0;
                z[j] = 0;
            }

            mMaxX = 0.0f;
            mMaxY = 0.0f;
            mMaxZ = 0.0f;
        }
        invalidate();
    }

    private int data2color(int a, float x, float y, float z) {
        return Color.argb(a, (int) ((x - mMaxX) / 2.0 / mMaxX * 255),
                (int) ((y - mMaxY) / 2.0 / mMaxY * 255),
                (int) ((z - mMaxZ) / 2.0 / mMaxZ * 255));
    }

    public void add(float data[]) {
        synchronized (mDataLock) {
            for (int i = 1; i < x.length; i++) {
                x[i - 1] = x[i];
                y[i - 1] = y[i];
                z[i - 1] = z[i];
            }
            x[x.length - 1] = data[0];
            y[y.length - 1] = data[1];
            z[z.length - 1] = data[2];

            mMaxX = Math.max(mMaxX, Math.abs(data[0]));
            mMaxY = Math.max(mMaxY, Math.abs(data[1]));
            mMaxZ = Math.max(mMaxZ, Math.abs(data[2]));

            if (mMaxX == 0.0f) {
                mMaxX = 1.0f;
            }
            if (mMaxY == 0.0f) {
                mMaxY = 1.0f;
            }
            if (mMaxZ == 0.0f) {
                mMaxZ = 1.0f;
            }
        }
        invalidate();
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth() - getPaddingLeft() - getPaddingRight();
        int height = getHeight() - getPaddingBottom() - getPaddingTop();

        int radius = ((Math.min(width, height)));

        int minus = Math.abs(width - height) / 2;

        float cx = width < height ? 0 : minus;//width / 2.0f;
        float cy = width > height ? 0 : minus;//height / 2.0f;
        mRectF.set(cx, cy, cx + radius, cy + radius);

        float mCurArc = 0;
        for (int i = 0; i < MAX_LENGTH; i++) {
            mArcPaint.setColor(data2color(0, x[i], y[i], z[i]));
            canvas.drawArc(mRectF, mCurArc, mCurArc + mDr, true, mArcPaint);
            mCurArc += mDr;
        }
    }
}
